<template>
  <div class="app-container">
    <el-card v-show="showSearch" style="margin-bottom: 20px">
      <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="90px">
        <el-row :gutter="24">
          <el-col :lg="8" :md="8" :sm="12" :xl="8" :xs="24">
            <el-form-item label="字段中文名" prop="extraChinese">
              <el-input
                v-model="queryParams.extraChinese"
                clearable
                placeholder="请输入额外字段中文名称"
                size="small"
                style="width: 100%"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="8" :md="8" :sm="24" :xl="8" :xs="24">
            <el-row justify="end" type="flex">
              <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
              <el-button icon="el-icon-search" size="small" type="cyan" @click="handleQuery">搜索</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <base-table :columns="fieldColumns" :data="fieldList"
                  :props-delete="fieldDelete"
                  :props-init="getList"
                  :props-save="fieldSave"
                  :rules="fieldRules"
      />
      <pagination
        v-show="total>0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import BaseTable from '@/components/baseTable'
import { addExtraField, delExtraField, listExtraField, updateExtraField } from '@/api/system/extraField'

export default {
  name: 'Index',
  components: {
    BaseTable
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租赁类型数据字典
      leaseTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        code: undefined
      },
      fieldList: [],
      fieldColumns: [
        {
          prop: 'extraTable',
          label: '关联模块',
          type: 'SELECT',
          typeOptions: [
            {
              dictLabel: '用户表',
              dictValue: 'sys_user'
            },
            {
              dictLabel: '部门表',
              dictValue: 'sys_dept'
            }
          ]
        },
        {
          prop: 'extraType',
          label: '字段类型',
          type: 'SELECT',
          typeOptions: [
            {
              dictLabel: 'varchar',
              dictValue: 'varchar'
            },
            {
              dictLabel: 'decimal',
              dictValue: 'decimal'
            },
            {
              dictLabel: 'date',
              dictValue: 'date'
            }
          ]
        },
        {
          prop: 'extraChinese',
          label: '字段中文名',
          type: 'INPUT'
        },
        {
          prop: 'extraName',
          label: '字段英文名',
          type: 'INPUT'
        },
        {
          prop: 'extraDict',
          label: '字典名',
          type: 'INPUT'
        }
      ],
      fieldRules: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      listExtraField(this.queryParams).then(response => {
        this.fieldList = response.rows
      })
    },
    fieldSave(row) {
      if (row.id !== undefined) {
        return updateExtraField(row)
      } else {
        return addExtraField(row)
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 删除按钮操作 */
    fieldDelete(row) {
      this.$confirm('是否确认删除字段名称为为"' + row.extraName + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delExtraField(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form-item {
  width: 100%;
  display: flex;
}
</style>
<style lang="scss">
.el-form-item--medium {
  .el-form-item__content {
    flex: 1;
  }
}
</style>
